<?php

namespace App\Http\Controllers\Api;

use App\Http\Controllers\Controller;
use App\Models\Fav;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

class FavController extends Controller
{
    //添加收藏
    public function add(Request $request)
    {
        try {
            //表单验证
            $postData = $this->validate($request,[
                'openid'=>'required',
                'fang_id'=>'required|numeric'
            ]);
            //入库
            //1.查询此用户对此房源有过收藏
            $data = Fav::where('openid',$postData['openid'])->where('fang_id',$postData['fang_id'])->first();
            $postData['fang_id'] = (int)$postData['fang_id'];
            //2.判断是否收藏与否，作出对应的操作
            if(is_null($data)){
                //此用户没有收藏过此房源信息
                $data = Fav::create($postData);
            }else{
                //数据存在更新一个时间就可以，不需要添加
                Fav::where('openid',$postData['openid'])->where('fang_id',$postData['fang_id'])->update($postData);
                #$data = array_merge($data->toArray(),$postData);
            }
            return response()->json(['status'=>0,'msg'=>'添加成功','data'=>$data],201);
        }catch (\Exception $exception){
            return ['status'=>100010,'msg'=>'数据验证不通过'];
        }
    }
    // 查询用户是否有收藏房源
    public function show(Request $request) {
        $openid = $request->get('openid');
        $fang_id = $request->get('fang_id');
        $ret = Fav::where('openid', $openid)->where('fang_id', $fang_id)->first();
        DB::table('favs')->where('openid', $openid)->where('fang_id', $fang_id)->delete();
        if (is_null($ret)) {
            return ['status' => 0];
        }
        return ['status' => 1];
    }
}
